package com.bjpowernode;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) throws SQLException {
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        String driver = bundle.getString("jdbc.driver");
        String url = bundle.getString("jdbc.url");
        String username = bundle.getString("jdbc.username");
        String password = bundle.getString("jdbc.password");
        Connection conn = null;
        PreparedStatement pstmt = null;
        Statement stmt = null;
        ResultSet rs = null;
        /*
         package com.mysql.cj.jdbc.Driver;类 使用了静态代码块，代码启动运行怎执行静态代码块。
         static {
            try {
                java.sql.DriverManager.registerDriver(new Driver());
            } catch (SQLException E) {
                throw new RuntimeException("Can't register driver!");
            }
        }
        使用反射机制， 执行静态代码块，加载驱动，
        */
        try {
            //1. 注册驱动
            Class.forName(driver);
            //2. 获取连接
            conn = DriverManager.getConnection(url,username,password);
            //3. 创建爱你数据库操作对象
            //pstmt = conn.prepareStatement("insert into t_user value(null,'天权星','123','凝光')");
            stmt = conn.createStatement();
            String sql = "select * from t_user";
            //4. 执行sql
            //int i = pstmt.executeUpdate();
            //System.out.println(i);
            rs = stmt.executeQuery(sql);
            //5. 处理查询结果集对象
            if (rs.isBeforeFirst()) {
                while (rs.next()) {
                    // getString() 方法不管数据是什么类型， 通通返回String类型，并且参数从 1 开始，表示第几列
                    /*String id = rs.getString(1);
                    String loginName = rs.getString(2);
                    String loginPwd = rs.getString(3);
                    String realName = rs.getString(4);*/


                    // 建议写列名，是以列的名字查询，重命名之后要写重命名的名字
                    /*String id = rs.getString("id");
                    String loginName = rs.getString("loginName");
                    String loginPwd = rs.getString("loginPwd");
                    String realName = rs.getString("realName");*/

                    // 根据列的类型获取对象
                    int id = rs.getInt("id");
                    String loginName = rs.getString("loginName");
                    String loginPwd = rs.getString("loginPwd");
                    String realName = rs.getString("realName");
                    System.out.printf("id: %s, loginName: %s, loginPwd: %s, realName: %s %n",id,loginName,loginPwd,realName);
                }
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            //6. 释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
